export function createTable(tableName, columnsData){
    let columnsSQL = createColumnsSQL(columnsData);
    let sql = "# 自动生成\n" +
        "create table if not exists " + "`" + tableName + "` (\n" +
        columnsSQL +
        ")ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci;"
    return sql
}

export function createInsertSQL(tableName, dataList, columnsData){
    let sql = "";
    let len = dataList.length;
    for (let i = 0; i < len; i++) {
        let oneSQL = "INSERT INTO " + tableName + " ";
        let colSQL = "(";
        let valSQL = "(";

        for (let j = 0; j < columnsData.length; j++) {
            colSQL = colSQL + "`" + columnsData[j].columnName + "`";
            valSQL = valSQL + (columnsData[j].typeVal == '1' || columnsData[j].typeVal === '2' ? "\"" : '') +
                dataList[i][columnsData[j].columnName] + (columnsData[j].typeVal == '1' || columnsData[j].typeVal == '2' ? "\"" : '');
            if(j != columnsData.length - 1){
                colSQL = colSQL + ", ";
                valSQL = valSQL + ", ";
            }
        }
        sql = sql + oneSQL + colSQL + ") values" + valSQL + ");\n";
    }
    return sql;
}

export function formatJSON(rawText){
    rawText = rawText.replaceAll("[", "[\n  ");
    rawText = rawText.replaceAll("]", "\n]");
    rawText = rawText.replaceAll("},{", "},\n  {");
    rawText = rawText.replaceAll("{", "{\n    ");
    rawText = rawText.replaceAll("}", "\n  }");
    rawText = rawText.replaceAll(",\"", ",\n    \"");
    return rawText;
}


export function createJavaEntity(tableName, columnsData){
    let javaCode = '@Data\npublic class ' + tableName + " {\n";
    let construct = '';
    let constructInner = '';
    for (let i = 0; i < columnsData.length; i++) {
        let t = (columnsData[i].comment != '' ? ("\n  /**\n   * " + columnsData[i].comment + "\n   */") : '');
        t = t + "\n  private " + getType(columnsData[i].typeVal) + " " + columnsData[i].columnName + ";\n";
        let t1 = getType(columnsData[i].typeVal) + " " + columnsData[i].columnName;
        let t3 ="    this." + columnsData[i].columnName + " = " + columnsData[i].columnName + ";\n";
        if(i != columnsData.length - 1){
            t1 = t1 + ', ';
        }
        javaCode = javaCode + t;
        construct = construct + t1;
        constructInner = constructInner + t3;
    }
    javaCode = javaCode + "\n  public " + tableName + "(" + construct + ") {\n" + constructInner + "  }\n}";
    return javaCode;
}

export function createJavaData(tableName, dataList, columnsData){
    let javaCode = "List<" + tableName + "> data = new ArrayList<>();\n\n";
    let len = dataList.length;
    for (let i = 0; i < len; i++) {
        let addCode = "data.add(new " + tableName + "(";
        for (let j = 0; j < columnsData.length; j++) {

            addCode = addCode + (columnsData[j].typeVal == '1' || columnsData[j].typeVal == '2' ? "\"" : '') +
                dataList[i][columnsData[j].columnName] +
                (columnsData[j].typeVal == '1' || columnsData[j].typeVal == '2' ? "\"" : '');

            if(j != columnsData.length - 1){
                addCode = addCode + ", ";
            }
        }
        javaCode = javaCode + addCode + ");\n";
    }
    return javaCode;
}

export function createSQLToColumn(sql){

}

function getType(type){
    switch (type){
        case "0":
        case "3": {
            return "Integer";
        }
        case "1": {
            return "String";
        }
        case "2": {
            return "Date";
        }
    }
}

function createColumnsSQL(columnsData){
    let sql = '';
    for (let i = 0; i < columnsData.length; i++) {
        let t = "  `" + columnsData[i].columnName + "` " + (columnsData[i].type == 'varchar' ? 'varchar(255)' : columnsData[i].type) + " not null comment \'" + columnsData[i].comment + "\'"
        if(i != columnsData.length - 1){
            t = t + ',';
        }
        sql = sql + t + "\n"
    }
    return sql;
}



export default {
    createTable,
    createInsertSQL,
    formatJSON,
    createJavaEntity,
    createJavaData,
}